import {
    mapState,
    mapMutations
} from 'vuex'
import variables from '@/static/js/variables.js'

export default {
    data() {
        return {
            pageInfo: {
                index: 0,
                paramsKey: ''
            },
        }
    },
    computed: {
        currentSyr() {
            return this.$store.state.currentSyr
        },
        currentUser() {
            return this.$store.state.currentUser
        },
    },
    mounted() {
        // this.init()
    },
    methods: {
        //导航到该页面，需要向页面中填充已有的信息
        init() {
            console.log('数据库返回的数据：', 'abc')
            let fileCode = this.name
            console.log(fileCode, variables.pageMap.get(fileCode))
            let dataKey = variables.pageMap.get(fileCode)
            if (dataKey) {

                // 数据库操作
                let db = wx.cloud.database(),
                    that = this
                db.collection('JCRS')
                    .where({
                        openid: this.currentSyr.openid,
                        name: this.currentSyr.name
                    }).get({
                    success: res => {
                        console.log('数据库返回的数据：', res.data)
                    }
                })
            }
        },
        // 直接跳转到下一页面，不提交信息。
        jump(nextRoute) {
            if (nextRoute === 300) {
                nextRoute = nextRoute + 'fpjqr'
            } else {
                nextRoute = nextRoute + 'syrfpj'
            }

            uni.navigateTo({
                url: `/pages/${nextRoute}/${nextRoute}`,
                success() {
                    console.log('jump succ')
                },
                fail() {
                    console.log('jump failed')
                }
            })
        },
        // 提交数据并跳转到下一页
        submitData(data, nextRoute) {
            // 没有数据，意思是从欢迎页面跳转到第一个填写受益人信息的页面。
            if (data === null) {
                uni.navigateTo({
                    url: '/pages/100syr/100syr',
                    success: function success() {
                        console.log('succ');
                    },
                    fail: function fail() {
                        console.log('failed');
                    }
                });
                return
            }
            if (nextRoute === '300') {
                nextRoute = nextRoute + 'fpjqr'
            } else {
                nextRoute = nextRoute + 'syrfpj'
            }
            // 数据库操作
            let db = wx.cloud.database(),
                that = this
            db.collection('JCRS')
                .where({
                    openid: this.currentSyr.openid,
                    name: this.currentSyr.name
                })
                .update({
                    data: {
                        [data.key]: data.data,
                        finished: '300fpjqr' === nextRoute
                    }
                })
                .then(() => {
                    uni.redirectTo({
                        url: `/pages/${nextRoute}/${nextRoute}`,
                        success() {
                            console.log(nextRoute + 'succ')
                        },
                        fail() {
                            console.error(nextRoute + 'failed')
                        }
                    })
                })

        }
    }
}
